package com.kefu.admin.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 静态工具方法，日期操作
 * 
 * @author liuwei
 * @version 1.0.0 Creation date:2014年08月19日 10:16:12
 * 
 * SimpleDateFormat是线程非安全的，使用此工具需要注意
 */

public class DateUtils {
	private static final Logger log = LoggerFactory.getLogger(DateUtils.class);

	/* 默认日期格式 */
	private static String format = "yyyy-MM-dd HH:mm:ss";

	private static String format14 = "yyyyMMddHHmmss";

	private static String format8 = "yyyyMMdd";
	private static final String formathms = "HH:mm:ss";
	private static final SimpleDateFormat sdfhms = new SimpleDateFormat(formathms);
	private static final SimpleDateFormat sdfymdhms = new SimpleDateFormat(format);
	private static String format10 = "yyyy-MM-dd";


	/**
	 * 获得当前时间字符串，格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @return 当前时间字符串
	 */
	public static String getCurrentTimeAsString() {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	/**
	 * 获得当前时间的14位字符串，格式为yyyyMMddHHmmss
	 * 
	 * @return 当前时间14位字符串
	 */
	public static String getCurrentTimeAs14String() {
		SimpleDateFormat sdf = new SimpleDateFormat(format14);
		return sdf.format(new Date());
	}
	
	/**
	 * 获得当前时间自定义时间格式
	 */
	public static String getCurrentTimeAsDIYString(String frmat) {
		SimpleDateFormat sdf = new SimpleDateFormat(frmat);
		return sdf.format(new Date());
	}

	/**
	 * 获得当前时间的8位字符串，格式为yyyyMMdd
	 * 
	 * @return 当前时间8位字符串
	 */
	public static String getCurrentTimeAs8String() {
		SimpleDateFormat sdf = new SimpleDateFormat(format8);
		return sdf.format(new Date());
	}





	private static boolean isInZone(long tStart, long tEnd, long t) throws ParseException {
		return tStart <= t && t <= tEnd;
	}

	private static long getLong(String timeStr) throws ParseException {
		return sdfhms.parse(timeStr).getTime();
	}

	private static long getCurrentTime() throws ParseException {
		return getLong(sdfhms.format(new Date()));
	}
	
	public static String getCurrentTime(String fmat){
		SimpleDateFormat sdf=new SimpleDateFormat(fmat);
		return sdf.format(new Date());
	}

	/**
	 * 当前时间必须大于要判断时间至少一个月
	 * */
	public static boolean dateDaYuMonth(String sourceTime){
		Calendar sourceCalendar = DateUtils.getCalendarObj(sourceTime,"yyyy-MM-dd HH:mm:ss");
		int sourceYear = sourceCalendar.get(Calendar.YEAR);
		int sourceMonth = sourceCalendar.get(Calendar.MONTH)+1;
		Calendar nowCalendar = Calendar.getInstance();
		int nowYear = nowCalendar.get(Calendar.YEAR);
		int nowMonth = nowCalendar.get(Calendar.MONTH)+1;
		if(nowYear>=sourceYear && nowMonth>sourceMonth){
			//当前时间必须大于要判断时间至少一个月
			return true;
		}
		return false;
	}
    
    public static void main(String[] args) {
    	String sourceTime="2018-08-29 17:06:43";


		Calendar sourceCalendar = getCalendarObj(sourceTime,"yyyy-MM-dd HH:mm:ss");
		int sourceYear = sourceCalendar.get(Calendar.YEAR);
		int sourceMonth = sourceCalendar.get(Calendar.MONTH)+1;

		Calendar nowCalendar = Calendar.getInstance();
		int nowYear = nowCalendar.get(Calendar.YEAR);
		int nowMonth = nowCalendar.get(Calendar.MONTH)+1;


		System.out.println("sourceYear"+sourceYear);
		System.out.println("sourceMonth"+sourceMonth);
		System.out.println("nowYear"+nowYear);
		System.out.println("nowMonth"+nowMonth);

		if(nowYear>=sourceYear && nowMonth>sourceMonth){
			//当前时间必须大于要判断时间至少一个月
			System.out.println("ok 大于了");


		}else{
			System.out.println("不大于");
		}


	}


	/**
	 * 将一个Date类型时间转换为Calendar对象
	 * */
	public static Calendar getCalendarObj(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * 将一个String类型时间转换为Calendar对象
	 * */
	public static Calendar getCalendarObj(String sourceTime,String pattern){
		Date date = DateUtils.parseToDateTime(sourceTime,pattern);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}
    
    /** 
     * 判断某一时间是否在一个区间内 
     *  
     * @param sourceTime 
     *            时间区间,半闭合,如[10:00-20:00) 
     * @param curTime 
     *            需要判断的时间 如10:00 
     * @return  
     * @throws IllegalArgumentException 
     */  
    public static boolean isInTime(String sourceTime, String curTime) {  
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {  
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);  
        }  
        if (curTime == null || !curTime.contains(":")) {  
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);  
        }  
        String[] args = sourceTime.split("-");  
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");  
        try {  
            long now = sdf.parse(curTime).getTime();  
            long start = sdf.parse(args[0]).getTime();  
            long end = sdf.parse(args[1]).getTime();  
            if (args[1].equals("00:00")) {  
                args[1] = "24:00";  
            }  
            if (end < start) {  
                if (now >= end && now < start) {  
                    return false;  
                } else {  
                    return true;  
                }  
            }   
            else {  
                if (now >= start && now < end) {  
                    return true;  
                } else {  
                    return false;  
                }  
            }  
        } catch (ParseException e) {  
            e.printStackTrace();  
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);  
        }  
      
    }  
	
	/**
	 * 周六周日判断
	 *
	 * @param date
	 * @return
	 */
	public static boolean isWeekend(Date date) {
		boolean weekend = false;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			weekend = true;
		}
		return weekend;
	}
	public static boolean isWeekend() {
		return isWeekend(new Date());
	}
	

	
	/**
	 * 获取到当前日期是星期几(中国式星期)
	 * @return
	 */
	public static Integer getWeekend() {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			Integer week = cal.get(Calendar.DAY_OF_WEEK);
			Integer tempWeek = week - 1;
			return tempWeek == 0 ? 7 : tempWeek;
		} catch (Exception e) {
		}
		return 0;
	}

	/**
	 * 给定一 字符串日期获取到日期是星期几(中国式星期)
	 * @param yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Integer getWeekend(String date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(date));
			Integer week = cal.get(Calendar.DAY_OF_WEEK);
			Integer tempWeek = week - 1;
			return tempWeek == 0 ? 7 : tempWeek;
		} catch (Exception e) {
		}
		return 0;
	}
	
	
	// 获取当前时间所在年的周数
	public static int getWeekOfYear(){
		 Calendar c = new GregorianCalendar();
         c.setFirstDayOfWeek(Calendar.MONDAY);
         c.setMinimalDaysInFirstWeek(7);
         c.setTime(new Date());
         return c.get(Calendar.WEEK_OF_YEAR);
	}
	
	/***
	 * 传入日期获取是当年的第几周
	 * */
    public static int getWeekOfYear(String date) {
    	try {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = new GregorianCalendar();
            c.setFirstDayOfWeek(Calendar.MONDAY);
            c.setMinimalDaysInFirstWeek(7);
            c.setTime(sdf.parse(date));
     
            return c.get(Calendar.WEEK_OF_YEAR);
		} catch (Exception e) {
		}
    	return 0;
    }



	/**
	 * 将指定日期转换为字符串，格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date 日期
	 * @return 日期字符串
	 */
	public static String DateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return date == null ? null : sdf.format(date);
	}

	/**
	 * 将指定日期转换为字符串，格式为yyyy-MM-dd
	 * 
	 * @param date 日期
	 * @return 日期字符串
	 */
	public static String DateToStringtype(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return date == null ? null : sdf.format(date);
	}

	/**
	 * 将指定日期转换为字符串，格式为yyyy年MM月dd日 HH:mm:ss
	 * 
	 * @param date 日期
	 * @return 日期字符串
	 */
	public static String DateToStringRecord(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return date == null ? null : sdf.format(date);
	}

	/**
	 * 功能描述: 将指定日期转换为字符串，日期格式自定义
	 * 
	 * @param date
	 * @param format
	 * @return
	 * @author zhangjf
	 * @Time 2014年12月25日 上午9:50:58
	 */
	public static String DateToStringByFormat(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return date == null ? null : sdf.format(date);
	}

	/**
	 * 将日期转换为指定格式的字符串
	 * 
	 * @param date 日期
	 *            ，format格式
	 * @return 日期字符串
	 */
	public static String DateToString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return date == null ? null : sdf.format(date);
	}

	/**
	 * 将字符串转换为指定格式的日期时间
	 * 
	 * @param strDate
	 *            日期字符串
	 * @param pattern
	 *            日期格式，与strDate格式匹配
	 */
	public static Date parseToDateTime(String strDate, String pattern) {
		try {
			return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(pattern).parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}



	/**
	 * 将字符串转换为指定格式的日期字符串
	 * 
	 * @param strDate
	 *            日期字符串
	 * @param pattern
	 *            日期格式，与strDate格式匹配
	 * @param format 要转换成的日期格式
	 * @return
	 */
	public static String parseToDateTime(String strDate, String pattern, String format) {
		Date date = parseToDateTime(strDate, pattern);
		if (date == null) {
			return "";
		}
		return DateToString(date, format);
	}

	/**
	 * 将字符串转换为默认格式的日期字符串（yyyyMMddHHmmss）
	 * 
	 * @param strDate
	 *            日期字符串
	 * @param pattern
	 *            日期格式，与strDate格式匹配
	 * @return
	 */
	public static String parseTo14DateTime(String strDate, String pattern) {
		Date date = parseToDateTime(strDate, pattern);
		if (date == null) {
			return "";
		}
		return DateToString(date, format14);
	}

	/**
	 * 将字符串转换为默认格式的日期字符串（yyyyMMddHHmmss）
	 * 
	 * @param strDate
	 *            日期字符串
	 * @param pattern
	 *            日期格式，与strDate格式匹配
	 * @return
	 */
	public static String parseTo8DateTime(String strDate, String pattern) {
		Date date = parseToDateTime(strDate, pattern);
		if (date == null) {
			return "";
		}
		return DateToString(date, format8);
	}

	/**
	 * 将字符串转换为默认格式的日期字符串（yyyyMMddHHmmss）
	 * 
	 * @param strDate
	 *            日期字符串
	 * @param format
	 *            要转换成的日期格式
	 * @return
	 */
	public static String parse14StringToDateTime(String strDate, String format) {
		return parseToDateTime(strDate, format14, format);
	}

	/**
	 * 将字符串转换为默认格式的日期字符串（yyyyMMddHHmmss to yyyy-MM-dd HH:mm:ss）
	 * 
	 * @param strDate
	 *            日期字符串
	 * @return
	 */
	public static String parse14StringToDate(String strDate) {
		return parseToDateTime(strDate, format14, format);
	}

	/*****
	 * 根据给定的时间获得该时间的毫秒数
	 * 
	 * @param date
	 *            传过来的时间值
	 * @return
	 */
	public static long getDateMesl(Date date) {
		long mselDate = 0;
		mselDate = date.getTime();
		return mselDate;
	}

	/******
	 * 根据字符串获得毫秒数
	 * 
	 * @param dateStr
	 *            时间字符串
	 * @param formate
	 *            yyyyMMddHHmmss
	 * @return
	 */
	public static long getDateHaoMiao(String dateStr, String formate) {
		SimpleDateFormat sdf = new SimpleDateFormat(formate);
		long mseldate = 0;
		try {
			mseldate = sdf.parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return mseldate;
	}

	/*******
	 * 根据传入的时间字符串判断是否和当前时间是否相同
	 * 
	 * @param dateStr
	 *            时间字符串
	 * @param formate
	 *            yyyyMMddHHmmss
	 * @return
	 */
	public static boolean isToDay(String dateStr, String formate) {
		SimpleDateFormat sdf = new SimpleDateFormat(formate);
		String todayStr = sdf.format(new Date());
		if (todayStr != null && !todayStr.equals("") && dateStr != null && !dateStr.equals("")
				&& dateStr.equals(todayStr)) {
			return true;
		} else {
			return false;
		}
	}

	/*******
	 * 根据年月日构造一个日期
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @param day
	 *            日
	 * @return
	 */
	public static Date getDate(int year, int month, int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, day, 0, 0, 0);
		return cal.getTime();
	}

	/*******
	 * 根据日期，时分秒构造一个完整日期
	 * 
	 * @param date
	 *            日期
	 * @param hour
	 *            时
	 * @param min
	 *            分
	 * @param sec
	 *            秒
	 * @return 返回一个时间
	 */
	public static Date getDate(Date date, int hour, int min, int sec) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR, hour);
		cal.set(Calendar.MINUTE, min);
		cal.set(Calendar.SECOND, sec);
		return cal.getTime();
	}

	/*****
	 * 判断所给的时间是否为月尾
	 * 
	 * @param date
	 * @return Boolean
	 */
	public static boolean isEndOfTheMonth() {
		return isEndOfTheMonth(new Date());
	}
	/*****
	 * 判断所给的时间是否为月尾
	 * 
	 * @param date
	 * @return Boolean
	 */
	public static boolean isEndOfTheMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		if (cal.get(Calendar.DATE) == maxDay) {
			return true;
		} else {
			return false;
		}
	}

	/*******
	 * 判断所给的时间是否是年末
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isEndOfTheYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if ((cal.get(Calendar.MONTH) + 1 == 12) && (cal.get(Calendar.DATE) == 31)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据日期获得月尾
	 * 
	 * @param date
	 *            Date
	 * @return int
	 */
	public static final int getLastDayOfTheMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 判断日期是否为月首
	 * 
	 * @param date
	 *            Date
	 * @return true or false
	 */
	public static final boolean isStartOfTheMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		if (cal.get(Calendar.DATE) == 1) {
			return true;
		}

		return false;
	}

	/**
	 * 获得日期中的年
	 * 
	 * @param date
	 *            Date
	 * @return int
	 */
	public static final int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);

	}

	/**
	 * 获得日期中的月份
	 * 
	 * @param date
	 *            Date
	 * @return int
	 */
	public static final int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH);

	}

	/**
	 * 获得日期中天
	 * 
	 * @param date
	 *            Date
	 * @return int
	 */
	public static final int getDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DATE);

	}

	/**
	 * 给日期增加天数
	 * 
	 * @param date
	 *            日期
	 * @param numDays
	 *            增加的天数
	 * @return 增加后的日期
	 */
	public static Date addDaysToDate(Date date, int numDays) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, numDays);
		return c.getTime();
	}

	/**
	 * 给日期增加小时
	 * 
	 * @param date
	 *            日期
	 * @param numHours
	 *            增加的小时数
	 * @return 增加后的日期
	 */
	public static Date addHoursToDate(Date date, int numHours) {
		if (date == null) {
			return null;
		}

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR_OF_DAY, numHours);

		return c.getTime();
	}

	/**
	 * 给日期增加分钟
	 * 
	 * @param date
	 *            日期
	 * @param numMins
	 *            增加的分钟数
	 * @return 增加后的日期
	 */
	public static Date addMinutesToDate(Date date, int numMins) {
		if (date == null) {
			return null;
		}

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, numMins);

		return c.getTime();
	}

	/**
	 * 给日期增加月份
	 * 
	 * @param date
	 *            日期
	 * @param numMonths
	 *            增加的月数
	 * @return 增加后的日期
	 */
	public static Date addMonthsToDate(Date date, int numMonths) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, numMonths);
		return c.getTime();
	}

	/**
	 * 给日期增加年份
	 * 
	 * @param date
	 *            日期
	 * @param numYears
	 *            增加的年数
	 * @return 增加后的日期
	 */
	public static Date addYearsToDate(Date date, int numYears) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, numYears);
		return c.getTime();
	}

	/*****
	 * 判断是否为闰年
	 * 
	 * @param d
	 * @return
	 */
	public static boolean isLeap(Date d) {
		int y = getYear(d);
		return ((y % 4) == 0 && (y % 100) != 0) || (y % 400 == 0);
	}

	/*******
	 * 获取两个日期之间相差的天数
	 * 
	 * @param s1
	 *            小的时间字符串
	 * @param s2
	 *            大的时间字符串
	 * @param format
	 *            字符串时间的格式
	 * @return
	 */
	public static int hqxcts(String s1, String s2) {
		return hqxcts(s1,s2,"yyyy-MM-dd HH:mm:ss");
	}
	public static int hqxcts(String s1, String s2, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		Date d1 = null;
		Date d2 = null;
		try {
			d1 = df.parse(s1);
			d2 = df.parse(s2);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		int betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
		for (int i = 0; i < betweenYears; i++) {
			c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
			betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
		}
		return betweenDays;
	}

	/**
	 * 将长时间格式字符串转换为字符串 yyyy-MM-dd HH:mm:ss
	 */
	public static String toLongStr(String longStr) {
		Date date = new Date(Long.parseLong(longStr.trim()));
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 将长时间格式字符串转换为字符串 yyyy-MM-dd HH:mm:ss
	 */
	public static String toUnixtoJava(String longStr) {
		if (longStr.length() <= 10) {
			longStr += "000";
		}
		Date date = new Date(Convert.getLong(longStr.trim()));
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 将长时间格式字符串转换为字符串 fmat
	 */
	public static String toUnixtoJava(String longStr, String fmat) {
		if (longStr.length() <= 10) {
			longStr += "000";
		}
		Date date = new Date(Convert.getLong(longStr.trim()));
		SimpleDateFormat formatter = new SimpleDateFormat(fmat);
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 功能描述: 数据库日期类型转换字符串
	 * 
	 * @param resultList
	 * @param dateFieldName
	 *            (日期字段名)
	 * @return
	 * @author zhangjf
	 * @Time 2014年12月8日 下午2:51:26
	 */
	@SuppressWarnings("unchecked")
	public static List<Object> formateDateTimeList(List<Object> resultList, String dateFieldName) {
		if (resultList != null && resultList.size() > 0) {
			for (int i = 0; i < resultList.size(); i++) {
				Map<String, Object> temp = (Map<String, Object>) resultList.get(i);
				if (null != temp.get(dateFieldName) && !"".equals(temp.get(dateFieldName).toString().trim())) {
					formateDateTimeOne(temp, dateFieldName);
				}
			}
		}
		return resultList;
	}

	/**
	 * 功能描述: 数据库日期类型转换字符串
	 * 
	 * @param resultList
	 * @param dateFieldName
	 *            (日期字段名)
	 * @return
	 * @author liuwei
	 * @Time 2015-02-03 18:28:12
	 */
	public static List<Map<String, Object>> formateDateTimeListMap(List<Map<String, Object>> resultList,
			String dateFieldName) {
		if (resultList != null && resultList.size() > 0) {
			for (Map<String, Object> item : resultList) {
				if (null != item.get(dateFieldName) && !"".equals(item.get(dateFieldName).toString().trim())) {
					formateDateTimeOne(item, dateFieldName);
				}
			}
		}
		return resultList;
	}

	/**
	 * 获取两个日期之间相差的时间
	 * 
	 * @param start
	 * @param end
	 * @param type
	 *            day,hour,minute,seconds
	 * @return 指定类型的时间间隔
	 * @author liuwei
	 * @Time 2015-02-03 18:28:12
	 */
	public final static String TIME_BETWEEN_DAY = "TIME_BETWEEN_DAY";
	public final static String TIME_BETWEEN_HOUR = "TIME_BETWEEN_HOUR";
	public final static String TIME_BETWEEN_MINUTE = "TIME_BETWEEN_MINUTE";
	public final static String TIME_BETWEEN_SECONDS = "TIME_BETWEEN_SECONDS";

	public static int getBetweenTimes(Date start, Date end, String type) {
		long times = end.getTime() - start.getTime();
		if (type.equals(DateUtils.TIME_BETWEEN_DAY))
			return (int) times / (24 * 60 * 60 * 1000);
		if (type.equals(DateUtils.TIME_BETWEEN_HOUR))
			return (int) times / (60 * 60 * 1000);
		if (type.equals(DateUtils.TIME_BETWEEN_MINUTE))
			return (int) times / (60 * 1000);
		if (type.equals(DateUtils.TIME_BETWEEN_SECONDS))
			return (int) times / 1000;
		return 0;
	}
	
	public static int getBetweenTimes(Long start, Long end, String type) {
		long times = end-start;
		if (type.equals(DateUtils.TIME_BETWEEN_DAY))
			return (int) times / (24 * 60 * 60 * 1000);
		if (type.equals(DateUtils.TIME_BETWEEN_HOUR))
			return (int) times / (60 * 60 * 1000);
		if (type.equals(DateUtils.TIME_BETWEEN_MINUTE))
			return (int) times / (60 * 1000);
		if (type.equals(DateUtils.TIME_BETWEEN_SECONDS))
			return (int) times / 1000;
		return 0;
	}

	
	public static Integer getBetweenTimes(SimpleDateFormat HMS,String startTime,String endTime){
		try {
			Date start=HMS.parse(startTime);
			Date end=HMS.parse(endTime);
			return getBetweenTimes(start,end,TIME_BETWEEN_MINUTE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	
	
	
	/**
	 * 获取两个日期间隔的时间
	 * 
	 * @param start
	 * @param end
	 * @author liuwei
	 * @return 天，时，分，秒
	 * @Time 2015-02-03 18:28:12
	 */
	public static long[] getBetweenTimesDetail(Date start, Date end) {
		long[] result = new long[4];

		long offset = end.getTime() - start.getTime();
		result[0] = (long) (offset / (24 * 60 * 60 * 1000));
		offset = offset - result[0] * 24 * 60 * 60 * 1000;
		result[1] = (long) (offset / (60 * 60 * 1000));
		offset = offset - result[1] * 60 * 60 * 1000;
		result[2] = (long) (offset / (60 * 1000));
		offset = offset - result[2] * 60 * 1000;
		result[3] = (long) (offset / 1000);
		return result;
	}

	/**
	 * 功能描述: 数据库日期类型转换字符串,自定义日期类型（需和数据库列日期类型匹配）
	 * 
	 * @param resultList
	 * @param dateFieldName
	 * @param format
	 * @return
	 * @author zhangjf
	 * @Time 2014年12月25日 上午9:53:50
	 */
	@SuppressWarnings("unchecked")
	public static List<Object> formateDateTimeListByFormat(List<Object> resultList, String dateFieldName,
			String format) {
		if (resultList != null && resultList.size() > 0) {
			for (int i = 0; i < resultList.size(); i++) {
				Map<String, Object> temp = (Map<String, Object>) resultList.get(i);
				if (null != temp.get(dateFieldName) && !"".equals(temp.get(dateFieldName).toString().trim())) {
					formateDateTimeOneByFormat(temp, dateFieldName, format);
				}
			}
		}
		return resultList;
	}

	/**
	 * 功能描述: 将返回回来的Date类型转换成String类型
	 * 
	 * @param resultMap
	 * @param dateFieldName
	 *            (日期字段名)
	 * @return
	 * @author zhangjf
	 * @Time 2014年12月8日 下午2:50:56
	 */
	public static Map<String, Object> formateDateTimeOne(Map<String, Object> resultMap, String dateFieldName) {
		if (resultMap.containsKey(dateFieldName) && !"".equals(resultMap.get(dateFieldName).toString().trim())) {
			String data = DateUtils.DateToString((Date) resultMap.get(dateFieldName));
			data = timeCharacterFormat(data);
			resultMap.put(dateFieldName, data);
		}
		return resultMap;
	}

	/**
	 * 功能描述: 将返回回来的Date类型转换成String类型（需和数据库列日期类型匹配）
	 * 
	 * @param resultMap
	 *            传入包含日期类型的map数据
	 * @param dateFieldName
	 *            日期类型数据库列名
	 * @param dateFormate
	 *            日期格式
	 * @return
	 * @author zhangjf
	 * @Time 2014年12月25日 上午9:49:13
	 */
	public static Map<String, Object> formateDateTimeOneByFormat(Map<String, Object> resultMap, String dateFieldName,
			String dateFormat) {
		if (resultMap.containsKey(dateFieldName) && !"".equals(resultMap.get(dateFieldName).toString().trim())) {
			resultMap.put(dateFieldName,
					DateUtils.DateToStringByFormat((Date) resultMap.get(dateFieldName), dateFormat));
		}
		return resultMap;
	}

	/**
	 * 将指定日期转换为 自己需要格式的字符串
	 * 
	 * @param date日期
	 * @return 日期字符串
	 */
	public static String DateToStringFormat(String s1, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		try {
			d = formatter.parse(s1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String date = sdf.format(d);
		return date;
	}
	
	 /**
     * 将指定日期转换为 自己需要格式的字符串
     *
     * @param date日期
     * @return 日期字符串
     */
    public static String DateToStringFormat(String s1, String format,String format2) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date d = null;
        try {
            d = formatter.parse(s1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format2);
        String date = sdf.format(d);
        return date;
    }
    
    /**
   	 * 在days中寻找距离date最近的时间,但同时又不能大于date
   	 * @param days 时间比较集合
   	 * @param date 比较时间点
   	 * @return days中距离date最近的时间点的下坐标
   	 */
   	public static Integer getRecentDateIndex(List<Date> days, Date date) {
   		Date recentDay = days.get(0);
   		long distance = getDistance(recentDay.getTime(), date.getTime());
   		Date tempDate = null;
   		long tempDis = 0l;
   		Integer index=0;
   		for(int i=1;i<days.size();i++){
   			tempDate = days.get(i);
   			tempDis = getDistance(tempDate.getTime(), date.getTime());
   			if(tempDis<distance && tempDate.getTime()<date.getTime()){
   				distance = tempDis;
   				recentDay = tempDate;
   				index=i;
   			}
   		}
   		return index;
   	}
   	
   	/**
	 * 获取a和b之间的距离
	 * @param a
	 * @param b
	 * @return
	 */
	private static long getDistance(long a, long b) {
		if (a >= b) {
			return a - b;
		}
		return b - a;
	}	



	/*******
	 * 获取传入日期判断是否是今天,昨天,明天
	 * 
	 * @param s1
	 *            传入一个时间
	 * @return
	 */
	public static String timeCharacterFormat(String s1) {
		String s2 = getCurrentTimeAsString();
		int i = hqxcts(s2, s1, "yyyy-MM-dd HH:mm:ss");
		s2 = s1;
		s1 = DateToStringFormat(s1, "HH:mm:ss");
		String temp = "";
		if (i == -1) {
			temp = "昨天 " + s1;
		} else if (i == 0) {
			temp = "今天 " + s1;
		} else if (i == 1) {
			temp = "明天 " + s1;
		} else {
			temp = s2;
		}
		return temp;
	}

	/*******
	 * 将有昨天,今天,明天的转换为正常日期
	 * 
	 * @param s1
	 *            传入一个时间
	 * @return
	 */
	public static String timeCharacterToFormat(String s1) {
		java.util.Date nowdate = new java.util.Date();// 当前时间
		if (null != s1 && !"".equals(s1)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (s1.indexOf("今天") > -1) {
				String year = sdf.format(nowdate);
				s1 = s1.substring(2, s1.length());
				s1 = year + " " + s1;
			}
			if (s1.indexOf("昨天") > -1) {
				nowdate = DateUtils.addDaysToDate(nowdate, -1);
				String year = sdf.format(nowdate);
				s1 = s1.substring(2, s1.length());
				s1 = year + " " + s1;
			}
			if (s1.indexOf("明天") > -1) {
				nowdate = DateUtils.addDaysToDate(nowdate, 1);
				String year = sdf.format(nowdate);
				s1 = s1.substring(2, s1.length());
				s1 = year + " " + s1;
			}
		}
		return s1;
	}

	/**
	 * 获取本周一日期
	 * 
	 * @return
	 */
	public static String getWeekF() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return DateUtils.DateToString(cal.getTime());
	}

	/**
	 * 获取本周日日期
	 * 
	 * @return
	 */
	public static String getWeekL() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		cal.add(Calendar.WEEK_OF_YEAR, 1);
		return DateUtils.DateToString(cal.getTime());
	}

	/**
	 * 获取本月第一天
	 * 
	 * @return
	 */
	public static String getMonthF() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return DateUtils.DateToString(cal.getTime());
	}

	/**
	 * 获取本月最后一天
	 * 
	 * @return
	 */
	public static String getMonthL() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return DateUtils.DateToString(cal.getTime());
	}

	/**
	 * 获取上个月月第一天
	 * 
	 * @return
	 */
	public static String getLMonthF() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return DateUtils.DateToString(cal.getTime());
	}

	public static Timestamp getTimestamp() {
		java.sql.Timestamp aDate = new java.sql.Timestamp(System.currentTimeMillis());

		return aDate;
	}

	/**
	 * 获取上个月最后一天
	 * 
	 * @return
	 */
	public static String getLMonthL() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DATE, -1);
		return DateUtils.DateToString(cal.getTime());
	}

	/**
	 * 比较两个时间大小 date1>date2 则返回true else false
	 * 
	 * @author liuwei
	 */
	public static boolean equestDate(String date1, String date2) {
		return equestDate(date1,date2,"yyyy-MM-dd HH:mm:ss");
	}
	public static boolean equestDate(String date1, String date2, String famt) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(famt);
			Date d1 = sdf.parse(date1);
			Date d2 = sdf.parse(date2);
			return d1.after(d2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}
}
